<template>
	<view class="login">
		<view class="header">
			<view class="black_1">
				<view class="line_1" @click="selectType()">
					<image class="img" src="/static/qieh.png"></image>
					<text class="text1">{{type?"登录ZY交易":"密码登录"}}</text>
				</view>

				<!-- 	<view class="line_2" >
					{{type?"密码登录":"短信登录"}}
				</view> -->
				<view class="line_4">
					<!-- <view class="left">0086</view> -->
					<uni-easyinput class="inputs" type="number" v-model="phone" :inputBorder="false"
						:styles="{backgroundColor: 'transparent'}" placeholder="请输入账号/手机号"
						placeholder-style="color:#9F9E9E;font-size:14px"></uni-easyinput>
				</view>
				<view style="border-bottom: 1rpx solid #DFDFDF;margin: 0px 10px;">
					
				</view>
				<view class="line_5">
					<uni-easyinput class="inputs" :type="type?'password':'text'" v-model="password" :inputBorder="false"
						:styles="{backgroundColor: 'transparent'}"
						:placeholder="type?'登录密码(忘记密码请回中应乡村APP进行修改)':'请输入验证码'"
						placeholder-style="color:#9F9E9E;font-size:14px"></uni-easyinput>
					<view v-if="!type" class="right" @click="sendCode()">{{codeText}}</view>
				</view>
<view style="border-bottom: 1rpx solid #DFDFDF;margin: 0px 10px;">
					
				</view>
				<!-- <view v-if="type" @click="forget()" class="line_6">
					忘记密码
				</view> -->
				<view class="line_7" @click="sumbit()">
					立即登录
				</view>
			<!-- 	<view class="line_8" @click="register()">
					注册账号
				</view> -->
			</view>

		</view>
		<!-- <view class="bottom">
			<view class="line_1">
				<uni-icons type="circle-filled" color="#fff" size="18"></uni-icons>
				<view class="text1">我已阅读并同意</view>
				<view class="text2" @click="agreement(0)">《服务协议》</view>和<view class="text2" @click="agreement(1)">《隐私协议》
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: true,
				phone: '',
				password: '',
				codeTime: 59,
				codeText: '发送',
				info: {
					describe: '',
					edition_url: '', //apk、wgt包下载地址或者应用市场地址  安卓应用市场 market://details?id=xxxx 苹果store itms-apps://itunes.apple.com/cn/app/xxxxxx
					edition_force: 0, //是否强制更新 0代表否 1代表是
					package_type: 1, //0是整包升级（apk或者appstore或者安卓应用市场） 1是wgt升级
					edition_issue: 1, //是否发行  0否 1是 为了控制上架应用市场审核时不能弹出热更新框
					edition_number: 100, //版本号 最重要的manifest里的版本号 （检查更新主要以服务器返回的edition_number版本号是否大于当前app的版本号来实现是否更新）
					edition_name: '1.0.0', // 版本名称 manifest里的版本名称
					edition_silence: 0, // 是否静默更新 0代表否 1代表是
				},
				inter: null

			};
		},
		onLoad() {
			this.update()
		},
		methods: {
			selectType() {
				this.type = !this.type
				this.password = ''
			},
			getTextCode() {
				var that = this
				if (this.inter)
					return
				this.inter = setInterval(function() {
					if (that.codeTime <= 0) {
						clearInterval(that.inter);
						that.inter = null
						that.codeText = "获取验证码"
						that.codeTime = 59
						return false;
					}
					// console.log(that.codeTime)
					that.codeText = that.codeTime + "秒"
					that.codeTime -= 1;
				}, 1000);
			},
			update() {
				//#ifdef APP-PLUS
				var that = this
				var form = uni.getSystemInfoSync().platform
				if (form == 'ios') {
					return
				}
				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, (inf) => {

					this.api.http.requests('common/webSetting', {}, res => {

						if (res.data.version) {
							console.log(res.data.version.version_code)
							console.log(inf.versionCode)
							if (Number(res.data.version.version_code) > Number(inf.versionCode)) {
								that.info.describe = res.data.version.version_update_content
								that.info.edition_url = res.data.version.version_download_url
								that.info.edition_number = res.data.version.version_code
								that.info.edition_name = res.data.version.version_code_name
								uni.navigateTo({
									url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
										JSON.stringify(that.info)
								});
							}
						}
					}, "POST")

				});
				//#endif

			},
			forget() {
				uni.navigateTo({
					url: '/pages/login/forgotPassword'
				})
			},
			register() {
				uni.navigateTo({
					url: '/pages/login/registerUser'
				})
			},
			agreement(index) {
				this.custom.tool.jump(0, 'my/agreement?type=' + index)
			},
			sendCode() {
				if (this.inter)
					return
				if (!this.phone) {
					uni.showToast({
						title: "请输入手机号",
						icon: 'none'
					})
					return
				}

				let data = {
					"mobile": this.phone,
					"event": "mobilelogin",
				}

				this.api.http.requests("sms/send", data, res => {
					console.log(res)
					if (res.code == 200) {
						this.getTextCode()
						uni.showToast({
							title: res.msg
						})
					}


				}, "POST")
			},
			sumbit() {
				if (!this.phone) {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
					return
				}


				if (!this.password) {
					uni.showToast({
						title: this.isLogin ? "请输入登录密码" : "请输入验证码",
						icon: "none"
					})
					return
				}
				// if (!this.selectxie) {
				// 	uni.showToast({
				// 		title: "请勾选协议",
				// 		icon: "none"
				// 	})
				// 	return
				// }
				let data = {
					"mobile": this.phone
				}
				if (this.type == 0) {
					data.captcha = this.password
				} else {
					data.password = this.password
				}

				let url = this.type == 1 ? "user/login" : "user/mobileLogin"
				uni.showLoading({

				})
				var that = this
				this.api.http.requests(url, data, res => {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					if (res.code == 200) {
						uni.setStorage({
							key: 'x-api-token',
							data: res.data.token,
							success: function() {
								uni.setStorageSync('isLogin', true);
								that.custom.tool.jump(3, 'trading/trading');
							},

							fail() {
								try {
									uni.setStorageSync('x-api-token', res.data.token);
								} catch (e) {

									console.log('token储存失败')
								}
							}
						});
					}
				}, "POST")
			}
		}
	}
</script>

<style lang="less">
	page {
		// background: url(@/static/bg2.png) no-repeat;
		// background-size: 100% 100%;
	}

	.header {
		// position: absolute;
		// margin-top: 100px;
		padding-top: 100px;
		// width: 100%;
		// display: flex;
	}

	.black_1 {
		// width: 80%;
		margin: 10px 20px;
		// background: #00000050;
		// border-radius: 15px 15px 15px 15px;
		// text-align: center;
		// padding: 20px 20px 0px 20px;

		.line_1 {
			// float: right;
			// right: 10px;
			// top: 10px;
			margin-bottom: 85px;

			font-size: 28px;
			font-weight: 400;

			color: #0A0A0A;

			.img {
				// vertical-align: middle;
				margin-right: 5px;
				width: 15px;
				height: 15px;
			}

			.text1 {
				line-height: 20px;
			}

		}

		.line_2 {
			font-size: 42rpx;
			font-weight: 800;
			color: #FFFFFF;
			margin-top: 40px;
			margin-bottom: 25px;
			text-align: start;
		}

		.line_5,
		.line_4 {
			display: flex;
			margin-top: 25px;
			align-items: center;
			background-color: #FFFFFF;
			// border-bottom: 1rpx solid #DFDFDF;
			// border-radius: 30px;
			padding: 0px 10px;

			.right {
				padding: 1px 8px;
				color: #FFFFFF;
				background: #FE903D;
				border-radius: 30px 30px 30px 30px;
			}

			.left {
				padding-right: 10px;
				border-right: 1px solid #A8A8A8;
			}
		}

		.inputs {
			text-align: start;
			padding: 5px 0px;
		}

		.line_6 {
			margin-top: 10px;
			float: right;
			font-size: 32rpx;
			font-weight: 400;
			color: #FFFFFF;
		}

		.line_7 {
			margin: 85px 2px 0px 2px;
			padding: 8px;
			background: #FE903D;
			border-radius: 24px;
			font-size: 35rpx;
			color: white;
			text-align: center;
		}

		.line_8 {
			margin: 20px 2px 0px 2px;
			border-radius: 14rpx;
			padding: 8px;
			border: 2rpx solid #FFFFFF;
			font-size: 35rpx;
			font-weight: bold;
			color: #FFFFFF;

		}


	}

	// .bottom {
	// 	position: absolute;
	// 	bottom: 20px;
	// 	width: 100%;
	// 	display: flex;

	// 	.line_1 {
	// 		margin: 10px auto;
	// 		display: flex;
	// 		font-size: 25rpx;
	// 		font-weight: 400;
	// 		color: white;
	// 	}
	// }
</style>